﻿using System.Collections.Generic;
using Network;

public class Logic 
{
    public uint EntityID { private set; get; }

    List<LogicComponent> components = new List<LogicComponent>();

    public static Logic InitLogicEntity(uint entityID, int entityNo)
    {
        //根据读表简历类
        Logic lEntity = new CharacterLogic(entityID);
        return lEntity;
    }

    public Logic(uint entityID)
    {
        EntityID = entityID;
    }

    public virtual void Destroy()
    {
        for (int i = 0; i < components.Count; i++)
        {
            components[i].Destroy();
        }
    }

    public T GetLogicComponent<T>() where T : class, LogicComponent 
    {
        for(int i = 0; i < components.Count; i++)
        {
            if (components[i] is T)
                return (T)components[i];
        }

        return null;
    }

    public void AddLogicComponent(LogicComponent component)
    {
        components.Add(component);
        component.Init(this);
    }

    public void Update(float deltaTime)
    {
        for(int i = 0; i < components.Count; i++)
        {
            components[i].DoUpdate(deltaTime);
        }
    }

    public void SendEntityMessage(uint opcode, object content)
    {
        entity_message entityMessage = new entity_message();
        entityMessage.entity_id = EntityID;
        entityMessage.message_id = opcode;
        entityMessage.content = ProtoUtil.SerializeProto(content);
        NetManager.Instance.SendGameMessage(OpCode.entity_message, ProtoUtil.SerializeProto(entityMessage));
    }
}
